package com.ruoyi.business.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.ShopOrderVo;
import com.ruoyi.business.service.IShopOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 订单Controller
 *
 * @author Tellsea
 * @date 2025-01-17
 */
@Api(tags = "订单Controller")
@RestController
@RequestMapping("/business/order")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Transactional(rollbackFor = Exception.class)
public class ShopOrderController extends BaseController {

    private final IShopOrderService shopOrderService;

    @ApiOperation("查询订单列表")
    //@PreAuthorize("@ss.hasPermi('business:order:list')")
    @GetMapping("/list")
    public TableDataInfo<ShopOrderVo> list(ShopOrderVo entity) {
        return shopOrderService.queryList(entity);
    }

    @ApiOperation("查询订单所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ShopOrderVo entity) {
        return AjaxResult.success("查询成功", shopOrderService.queryAll(entity));
    }

    @ApiOperation("导出订单列表")
    //@PreAuthorize("@ss.hasPermi('business:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopOrderVo entity) {
        List<ShopOrderVo> list = shopOrderService.queryAll(entity);
        ExcelUtil<ShopOrderVo> util = new ExcelUtil<>(ShopOrderVo.class);
        util.exportExcel(response, list, "订单数据");
    }

    @ApiOperation("获取订单详细信息")
    //@PreAuthorize("@ss.hasPermi('business:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success("查询成功", shopOrderService.queryById(orderId));
    }

    @ApiOperation("新增订单")
    //@PreAuthorize("@ss.hasPermi('business:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopOrderVo entity) {
        return toAjax(shopOrderService.save(entity));
    }

    @ApiOperation("修改订单")
    //@PreAuthorize("@ss.hasPermi('business:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopOrderVo entity) {
        return toAjax(shopOrderService.updateById(entity));
    }

    @ApiOperation("删除订单")
    //@PreAuthorize("@ss.hasPermi('business:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(shopOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
